package com.example.rapichofer;

import utils.BaseAsync;
import utils.Constantes;
import controladores.ControladorChoferes;
import activitis.TurnoActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class CalificarActivity extends Activity
{
	
	private long idViaje;
	private int calificacion;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_calificar);
		
		this.idViaje =  getIntent().getLongExtra("idViaje", -1);
		calificacion = Constantes.Calificaciones.NEUTRAL;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.calificar, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings)
		{
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void continuar(View v)
	{
		calificacion= Constantes.Calificaciones.NEUTRAL;
		new FinalizarViaje().execute();
	}
	
	public void calificacionBuena(View v)
	{
		calificacion= Constantes.Calificaciones.POSITIVA;
		new FinalizarViaje().execute();
	}
	
	public void calificacionMala(View v)
	{
		calificacion= Constantes.Calificaciones.NEGATIVA;
		new FinalizarViaje().execute();
	}
	
	private class FinalizarViaje extends BaseAsync
	{
		private boolean seguir;
		
		public FinalizarViaje ()
		{
			super (CalificarActivity.this, "Finalizando...");
		}
		
		@Override
		protected Void doInBackground(Void... params)
		{
			ControladorChoferes con = new ControladorChoferes();
			seguir = con.finalizarViaje(idViaje, calificacion);
			
			return null;
		}

		@Override
		protected void onPostExecute(Void result)
		{
			super.onPostExecute(result);
					
			if (seguir)
			{
				Intent intent = new Intent(CalificarActivity.this, TurnoActivity.class);
				startActivity(intent);
				finish();
			}
			else
			{
				AlertDialog alertDialog = new AlertDialog.Builder(CalificarActivity.this).create();
				alertDialog.setTitle("Advertencia!!!");
				alertDialog.setMessage("Error de conexion intente nuevamente");
				alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
						new DialogInterface.OnClickListener()
						{
							public void onClick(DialogInterface dialog, int which)
							{
								finish();
							}
						});
			}
		}
	}
}
